﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Maticsoft.DBUtility;

namespace ExtendBLL
{
    public class UserTable:LabMS.BLL.UserTable
    {
        /// <summary>
        /// 根据用户表里面的PCode获得人员表里面的人员名称
        /// </summary>
        /// <param name="PCode"></param>
        /// <returns></returns>
        public string SerachRealName(string PCode)
        {
            string str="Staff_Code='"+PCode+"'";
            LabMS.BLL.Staff bStaff = new LabMS.BLL.Staff();
            List<LabMS.Model.Staff> list = new List<LabMS.Model.Staff>();
            list = bStaff.GetModelList(str);
            if (list.Count <= 0)
            {
                return "";
            }
            else
            {
                return list[0].Staff_Name;
            }
        }



        /// <summary>
        /// 根据用户ID获得用户列表信息
        /// </summary>
        /// <param name="Userid"></param>
        /// <returns></returns>
        public List<LabMS.Model.UserTable> GetUserTableByUserID(int Userid)
        {
            LabMS.BLL.UserTable But = new LabMS.BLL.UserTable();
            return But.GetModelList("UserID="+Userid);
        }

        /// <summary>
        /// 从字典大类中获取小类编码，DClass
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public string GetDClassFromDictorary(string name)
        {
            string str = "Name='"+name+"'";
            LabMS.BLL.DictionaryClass BDC = new LabMS.BLL.DictionaryClass();
            return BDC.GetModelList(str)[0].Code;
        }

        /// <summary>
        /// 获得性别码,从基础字典里面
        /// </summary>
        /// <returns></returns>
        public string GetSexCode(string sexcode)
        {
            string DClass=GetDClassFromDictorary("性别");
            string str = "DClass='" + DClass + "' and Code='" + sexcode + "'";
            Dictionary BD = new Dictionary();
            List<LabMS.Model.Dictionary> listD = new List<LabMS.Model.Dictionary>();
            listD=BD.GetModelList(str);
            return listD[0].Name;  //如果Name表示的男，女
        }

        /// <summary>
        /// 根据Name返回DClass的值,假设性别的CODE是201，DClass=201
        /// </summary>
        /// <param name="Name">返回code的值</param>
        public string GetDcodeByName(string Name)
        {
            string str = "Name='"+Name+"'";
            List<LabMS.Model.Dictionary> listd = new List<LabMS.Model.Dictionary>();
            Dictionary dt = new Dictionary();
            listd = dt.GetModelList(str);
            return listd[0].Code;
        }

        /// <summary>
        /// 根据PCode获得人员真实名字
        /// </summary>
        /// <param name="PCode"></param>
        /// <returns></returns>
        public string GetRealNameByPCode(string PCode)
        {
            string str = "Staff_Code='" + PCode + "'";
            LabMS.BLL.Staff Bt = new LabMS.BLL.Staff();
            List<LabMS.Model.Staff> listT = new List<LabMS.Model.Staff>();
            listT = Bt.GetModelList(str);
            if (listT.Count <= 0)
            {
                return "";
            }
            else
            {
                return listT[0].Staff_Name;
            }
        }

        /// <summary>
        /// 根据人员的PCode从Staff表中得到用户表中没有的信息，如性别，年龄，邮箱，电话等
        /// </summary>
        /// <param name="PCode"></param>
        /// <returns></returns>
        public DataSet GetTeacherByPCode(string PCode)
        {
            string str = "Staff_Code='" + PCode + "'";
            LabMS.BLL.Staff BT = new LabMS.BLL.Staff();
            return BT.GetList(str);
        }

        /// <summary>
        /// 用户信息更新
        /// </summary>
        /// <param name="UserId"></param>
        public int UpdateUserTable(int UserId, string UserName, string PCode, string Password)
        {
            string str = "update UserTable set Password='" + Password + "',UserName='" + UserName + "',PCode='" + PCode + "' where UserId="+UserId+"";
            int count=Maticsoft.DBUtility.DbHelperSQL.ExecuteSql(str);
            return count;
        }

        public bool IsExistUserName(string UserName)
        {
            string str = "UserName='"+UserName+"'";
            int count;
            bool success = false;
            LabMS.BLL.UserTable BUT = new LabMS.BLL.UserTable();
            count=BUT.GetModelList(str).Count;
            if (count > 0)
            {
                //已经存在
                success = false;
            }
            else
            {
                //不存在，可以注册
                success = true;
            }
            return success;
        }

        static public bool ValidateUser(string UserName, string strUserPass)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from UserTable");
            strSql.Append(" where UserName=@UserName and Password=@Password ");
            SqlParameter[] parameters = {
					new SqlParameter("@UserName", SqlDbType.VarChar,50),
                    new SqlParameter("@Password", SqlDbType.VarChar,50)};
            parameters[0].Value = UserName;
            parameters[1].Value = strUserPass;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }
    }
}
